package org.edu.modules.sxjy.oss;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.File;
import java.io.FileOutputStream;

import org.edu.common.aspect.annotation.AutoLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author leidq
 * @create 2022-04-18 15:06
 */
@Api(tags = {"文件上传"})
@RestController
@RequestMapping("/sxjy/oss")
@ApiSort(60)
public class UploadController {
    private static final Logger log = LoggerFactory.getLogger(UploadController.class);
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @AutoLog("文件上传-文件上传")
    @ApiOperation(value = "文件上传", notes = "文件上传-文件上传")
    @PostMapping({"/uploadFile"})
    public FileDTO uploadBlog(@RequestParam("file") MultipartFile file) {
        String filename = file.getOriginalFilename();
        try {
            if ((file != null) &&
                    (!"".equals(filename.trim()))) {
                File newFile = new File(filename);
                FileOutputStream os = new FileOutputStream(newFile);
                os.write(file.getBytes());
                os.close();
                file.transferTo(newFile);

                return this.aliyunOSSUtil.upload(newFile);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
